package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Orders orders) {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    @GetMapping("/info")
    public AjaxResult info(){
        Orders orders = new Orders();
        List<Orders> list = ordersService.selectOrdersList(orders);
        List<Orders> collect = list.stream().sorted(Comparator.comparing(Orders::getCreateTime).reversed()).limit(10).collect(Collectors.toList());
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Orders orders1 : collect) {
            Map<String, Object> map = new HashMap<>();
            map.put("name",orders1.getName());
            map.put("type",orders1.getType());
            map.put("money",orders1.getMoney());
            mapList.add(map);
        }
        return AjaxResult.success(mapList);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(ordersService.selectOrdersById(id));
    }

    /**
     * 新增订单
     */
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders) {
        orders.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders) {
        orders.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }
}
